package com.whu.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.whu.aircraft.FlightTask;
import com.whu.aircraft.R;
import com.whu.aircraft.RoutePoint;
import com.whu.aircraft.TrackInfo;
import com.whu.service.impl.FlightTaskService;
import com.whu.vo.FlightTaskVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

@RestController
public class FlightTaskController {
    FlightTaskService flightTaskService;
    public static final int pageNum=8;

    @Autowired
    public FlightTaskController(FlightTaskService flightTaskService) {
        this.flightTaskService = flightTaskService;

    }
//    @RequestMapping("track/listByPage/{page}")
//    public R listByPage(@PathVariable int page){
//        IPage<TrackInfo> iPage = trackInfoService.listWithPage(page,1);
//        return R.ok().put("track_list",iPage.getRecords())
//                .put("pageCnt",iPage.getPages());
//    }
    @RequestMapping("flight_task/listByNameWithPage/{name}/{page}")
    public R listByNameWithPage(@PathVariable String name,@PathVariable int page){
        IPage<FlightTask> iPage = flightTaskService.listByNameWithPage(name,page,pageNum);
        return R.ok().put("flightTaskList",iPage.getRecords())
                .put("pageCnt",iPage.getPages());
    }
    @RequestMapping("flight_task/listByPage/{page}")
    public R listByPage(@PathVariable int page){
        IPage<FlightTask> iPage = flightTaskService.listWithPage(page,pageNum);
        return R.ok().put("flightTaskList",iPage.getRecords())
                .put("pageCnt",iPage.getPages());
    }
    @RequestMapping("/flight_task/list")
    public ArrayList<FlightTask> listAllTask(){
        return flightTaskService.listAllTask();
    }
    @RequestMapping("/flight_task/delete/{id}")
    public R delete(@PathVariable Long id){
        flightTaskService.deleteTaskById(id);
        return R.ok("删除成功！");
    }
    @RequestMapping("/flight_task/update")
    public R update(@RequestBody FlightTaskVO taskVO) throws JsonProcessingException, ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        FlightTask task = new FlightTask();
        task.setFlightTaskId(taskVO.getFlightTaskID());
        task.setFlightTaskName(taskVO.getFlightTaskName());
        task.setCreateTime(dateFormat.parse(taskVO.getCreateTime()));
        task.setUpdateTime(dateFormat.parse(taskVO.getUpdateTime()));
        task.setDefaultHeight(taskVO.getFlightTaskDefaultHeight());
        task.setDefaultVelocity(taskVO.getFlightTaskDefaultVelocity());
        ArrayList<RoutePoint> routes = new ArrayList<>();
        int pathLen = taskVO.getFlightTaskHeight().length;
        for(int i=0;i<pathLen;i++){
            RoutePoint rp = new RoutePoint();
            rp.setHeight(taskVO.getFlightTaskHeight()[i]);
            rp.setVelocity(taskVO.getFlightTaskVelocity()[i]);
            rp.setLongitude(taskVO.getFlightTaskPath()[i][0]);
            rp.setLatitude(taskVO.getFlightTaskPath()[i][1]);
            routes.add(rp);
        }
        task.setRoute(routes);
        task.route2Json();
        flightTaskService.updateTaskById(task);
        return R.ok("更新"+taskVO.getFlightTaskName()+"任务成功");
    }
    @RequestMapping("/flight_task/create")
    public R create(@RequestBody FlightTaskVO taskVO) throws ParseException, JsonProcessingException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        FlightTask task = new FlightTask();
        task.setFlightTaskName(taskVO.getFlightTaskName());
        task.setCreateTime(dateFormat.parse(taskVO.getCreateTime()));
        task.setUpdateTime(dateFormat.parse(taskVO.getUpdateTime()));
        task.setDefaultHeight(taskVO.getFlightTaskDefaultHeight());
        task.setDefaultVelocity(taskVO.getFlightTaskDefaultVelocity());
        ArrayList<RoutePoint> routes = new ArrayList<>();
        int pathLen = taskVO.getFlightTaskHeight().length;
        for(int i=0;i<pathLen;i++){
            RoutePoint rp = new RoutePoint();
            rp.setHeight(taskVO.getFlightTaskHeight()[i]);
            rp.setVelocity(taskVO.getFlightTaskVelocity()[i]);
            rp.setLongitude(taskVO.getFlightTaskPath()[i][0]);
            rp.setLatitude(taskVO.getFlightTaskPath()[i][1]);
            routes.add(rp);
        }
        task.setRoute(routes);
        task.route2Json();
        flightTaskService.insert(task);
        return R.ok("新增"+taskVO.getFlightTaskName()+"任务成功");
    }
}
